import { createWebHistory, createRouter } from "vue-router";
const FormMain = () => import("./views/FormMain.vue");
const LoginPage = () => import("./views/LoginPage.vue");
const LogoutPage = () => import("./views/LogoutPage");
const SignupPage = () => import("./views/SignupPage");
const MainPage = () => import("./views/MainPage.vue");
const ReqsPage = () => import("./views/ReqsPage.vue");
const UserPage = () => import("./views/UserPage.vue");
const UsersPage = () => import("./views/UsersPage.vue");
const ReqPage = () => import("./views/ReqPage.vue");
const OrganizationsPage = () => import("./views/OrganizationsPage.vue");
const OrganizationPage = () => import("./views/OrganizationPage.vue");
const MePage = () => import("./views/MePage.vue");
const VerificationPage = () => import("./views/VerificationPage.vue");
const RecoveryPage = () => import("./views/RecoveryPage.vue");
const NewPasswordPage = () => import("./views/NewPasswordPage.vue");

const routes = [
  {
    path: "/",
    name: "Домашняя страница",
    component: MainPage,
  },
  {
    path: "/recover",
    name: "Востановление пароля",
    component: RecoveryPage,
  },
  {
    path: "/newpass/:code",
    name: "Новый пароль",
    component: NewPasswordPage,
    props: (route) => {
      let code = route.params.code;
      return { code: code }
    }
  },
  {
    path: "/verification/:uuid",
    name: "Страница подтверждения почты",
    component: VerificationPage,
    props: (route) => {
      let uuid = route.params.uuid;
      return { uuid: uuid }
    }
  },
  {
    path: "/write",
    name: "Заполнение формы",
    component: FormMain,
  },
  {
    path: "/view/:page?",
    name: "Просмотр заявлений",
    component: ReqsPage,
    props: (route) => {
      let num = Number(route.params.page);
      if (num < 1 || isNaN(num)) {
        num = 1;
      }
      return { currentPage: num }
    }
  },
  {
    path: "/user/:id/:page?",
    component: UserPage,
    props: (route) => {
      let uuid = route.params.id;
      let page = Number(route.params.page);
      if (isNaN(page) || page < 1) {
        page = 1;
      }
      return { id: uuid, currentPage: page }
    }
  },
  {
    path: "/me",
    component: MePage,
  },
  {
    path: "/req/:id",
    component: ReqPage,
    props: (route) => {
      let num = Number(route.params.id);
      if (isNaN(num) || num < 0) {
        window.location.href = "/";
        return;
      }
      return { id: num }
    }
  },
  {
    path: "/users/:page?",
    name: "Просмотр пользователей",
    component: UsersPage,
    props: (route) => {
      let num = Number(route.params.page);
      if (num < 1 || isNaN(num)) {
        num = 1;
      }
      return { currentPage: num }
    }
  },
  {
    path: "/organizations/:page?",
    name: "Организации",
    component: OrganizationsPage,
    props: (route) => {
      let num = Number(route.params.page);
      if (num < 1 || isNaN(num)) {
        num = 1;
      }
      return { currentPage: num }
    }
  },
  {
    path: "/organization/:id/:page?",
    component: OrganizationPage,
    props: (route) => {
      let num = Number(route.params.id);
      let page = Number(route.params.page);
      if (isNaN(num) || num < 0) {
        window.location.href = "/";
        return;
      }
      if (isNaN(page) || page < 1) {
        page = 1;
      }
      return { id: num, currentPage: page }
    }
  },
  {
    path: "/login",
    name: "Страница логина",
    component: LoginPage,
  },
  {
    path: "/signup",
    name: "Страница регистрации",
    component: SignupPage,
  },
  {
    path: "/logout",
    name: "Страница разлогинивания",
    component: LogoutPage,
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }


});

export default router;
